package ca.jsbr.as2js_lib.display.canvas 
{
	import ca.jsbr.as2js_lib.utils.geom.Rect;
	import flash.events.Event;
	import js.element.canvas.CanvasRenderingContext2D;
	/**
	 * ...
	 * @author jsbr [me@jsbr.ca]
	 */
	public class Node2D extends View
	{
		
		protected var _childs:Vector.<View>;
		
		public function Node2D() 
		{
			_childs = new Vector.<View>();
		}
		
		
		public function addChild(view:View):void
		{
			if (view.parent)
				view.parent.removeChild(view)
			_childs.push(view);
			view.parent = this;
		}
		
		public function removeChild(view:View):void
		{
			var pos:int = _childs.indexOf(view);
			if (pos > -1)
			{
				_childs.splice(pos, 1);
				view.parent = null;
			}
			else
				trace("Child not found")
			view.destroy();
			return _childs.length-1
		}
		
		override public function destroy():void 
		{
			super.destroy();
			for (var i:int = 0; i < _childs.length;i++ )
			{
				var child:View = _childs[i];
				child.destroy();
			}
		}
		
		
		override function render(context:CanvasRenderingContext2D):void 
		{
			for (var i:int = 0; i < _childs.length;i++ )
			{
				var child:View = _childs[i];
				
				child.preRender(context);
				if (!stage)
					return;
				if (child.visible && this.parent)
					child.render(context);
				child.postRender(context);
			}
		}
		
	}

}